from GameLobbyViewHandler import GameLobbyViewHandler
# skin
from Shell.gui.Skin import SkinGet
# gui
from direct.gui.DirectGui import DirectFrame, DirectEntry, DirectButton
from Cube.gui.GameLobby import ChatRoomFrame, MapInfoFrame, MemberListControlFrame
from Cube.gui.DirectGUIPropertiesAccess import BtnTextSet, BtnCmdSet
from Cube.gui.QSizer import QSizer, ResizedCallBackSet, BoxSizer
from Cube.gui.DirectGUIPropertiesAccess import AlignText
# uiString
from Shell.data.UIString import uiString
from Cube.map import QMemberType

class GameLobbyViewInit(DirectFrame, GameLobbyViewHandler):
    def __init__(self,
                 model,
                 parent = None,
                 ):
        self.model = model
        self.MemberData = model.MemberListMgr
        mapHeader = model.MapHeader
        connectType = model.ConnectType

        DirectFrame.__init__(self, parent)
        GameLobbyViewHandler.__init__(self)

        ParameterDict = SkinGet("Game Lobby")
        param_sizerMainOuter = ParameterDict["main sizer outer"]
        param_ChatRoom = ParameterDict["chat room"]
        param_MapInfo = ParameterDict["map info"]
        param_MemberListControl = ParameterDict["member list control"]
        param_btn_HostIP = ParameterDict["btn_HostIP"]
        param_btn_HostIP["parent"] = self
        param_btn_StartQuit = ParameterDict["btn_StartQuit"]
        param_btn_StartQuit["parent"] = self

        memberTypeStringMap = {#QMemberType.HUMAN is reserved
                               QMemberType.COMPUTER:uiString[QMemberType.COMPUTER],
                               QMemberType.OPEN:uiString[QMemberType.OPEN],
                               QMemberType.CLOSE:uiString[QMemberType.CLOSE],
                               }

        self.ChatRoom = ChatRoomFrame(parent = self,
                                      param_textboard = param_ChatRoom["textBoardInfo"],
                                      param_textInputInfo = param_ChatRoom["textInputInfo"]
                                      )
        self.MapInfoFrame = MapInfoFrame(parent = self,
                                         miniatureInfo = param_MapInfo["miniatureInfo"],
                                         mapNameInfo = param_MapInfo["mapNameInfo"],
                                         mapInfoInfo = param_MapInfo["mapInfoInfo"],
                                         mapInfoData = mapHeader.MapInfo
                                        )
        self.MemberListControlFrame = MemberListControlFrame(parent = self,
                                                             param_btnTeamName = param_MemberListControl["btn_Team"],
                                                             param_labelMemberSymbol = param_MemberListControl["label_memberSymbol"],
                                                             param_labelMemberName = param_MemberListControl["label_memberName"],
                                                             param_optionMember = param_MemberListControl["option_memberName"],
                                                             param_sizerTeams = param_MemberListControl["sizer_teams"],
                                                             param_sizerMembers = param_MemberListControl["sizer_members"],
                                                             memberListData = mapHeader.MemberList,
                                                             connectType = connectType,
                                                             memberTypeStringMap = memberTypeStringMap
                                                             )

        # setup a Button for start game: server only
        if self.model.IsServerMode():
            self.btn_StartGame = DirectButton(**param_btn_StartQuit)
            BtnTextSet(self.btn_StartGame, uiString["Start Game"], param_btn_StartQuit["text_scale"])
        # setup a Button for cancel (Back to Main Menu)
        self.btn_Quit = DirectButton(**param_btn_StartQuit)
        BtnTextSet(self.btn_Quit, uiString["Quit"], param_btn_StartQuit["text_scale"])


        # setup a Button for host ip display
        self.btn_HostIP = DirectButton(**param_btn_HostIP)
        BtnTextSet(self.btn_HostIP, "Host IP", param_btn_HostIP["text_scale"])

        # TODO...
        frame_Banner = DirectFrame(parent = self,
                                   color = (0.6,0.6,0.6),
                                   frameSize = (-0.1,0.1,-0.1,0.1))

        # main-right
        sizerButtoms = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.02)
        sizerButtoms.Add(self.btn_Quit,1)
        if self.model.IsServerMode():
            sizerButtoms.Add(self.btn_StartGame,2)

        sizerMainRightBottom = BoxSizer(BoxSizer.VERTICAL, gap = 0.01)
        sizerMainRightBottom.Add(self.ChatRoom,1)
        sizerMainRightBottom.Add(sizerButtoms,0)

        sizerMainRight = BoxSizer(BoxSizer.VERTICAL, gap = 0.01)
        sizerMainRight.Add(self.MapInfoFrame,0)
        sizerMainRight.Add(sizerMainRightBottom,1)
        # main-left
        sizerMainLeft = BoxSizer(BoxSizer.VERTICAL, gap = 0.01)
        sizerMainLeft.Add(self.MemberListControlFrame,1)
        sizerMainLeft.Add(self.btn_HostIP,0,QSizer.ALIGN_LEFT)

        sizerMain = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.01)
        sizerMain.Add(sizerMainLeft)
        sizerMain.Add(sizerMainRight)

        sizerMainOuter = BoxSizer(BoxSizer.VERTICAL,
                                  gap = param_sizerMainOuter["gap"],
                                  size = param_sizerMainOuter["size"])
        sizerMainOuter.Add(frame_Banner,0)
        sizerMainOuter.Add(sizerMain,1)

        if self.model.IsServerMode():
            ResizedCallBackSet(self.btn_StartGame, AlignText, frame = self.btn_StartGame, align = "" )
        ResizedCallBackSet(self.btn_Quit, AlignText, frame = self.btn_Quit, align = "" )
        ResizedCallBackSet(self.btn_HostIP, AlignText, frame = self.btn_HostIP, align = "" )

        sizerMainOuter.Layout()

# destroy -----------------------------------------------------------
    def destroy(self):
        GameLobbyViewHandler.destroy(self)
        DirectFrame.destroy(self)
